﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;

namespace FYPDemo
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //string username = Session["username"].ToString();
            bindAdvertisementPeriod();
            bindAdvertisementPictureSize();
        }

        protected void bindAdvertisementPeriod()
        {
            DataTable dt = new DataTable();
            dt = AdvertisementManagement.listAdvertisementPeriod();

            ddlAdvPeriod.DataSource = dt;
            ddlAdvPeriod.DataTextField = "Period";
            ddlAdvPeriod.DataValueField = "Period";
            ddlAdvPeriod.DataBind();
        }

        protected void bindAdvertisementPictureSize()
        {
            DataTable dt = new DataTable();
            dt = AdvertisementManagement.listAdvertisementPictureSize();

            ddlAdvPictureSize.DataSource = dt;
            ddlAdvPictureSize.DataTextField = "PictureSize";
            ddlAdvPictureSize.DataValueField = "price";
            ddlAdvPictureSize.DataBind();
        }

        private void uploadFile(HttpPostedFile theFile, string uploadTo, string filename)
        {
            if (theFile != null && theFile.ContentLength > 0)
            {
                if (!Directory.Exists(Server.MapPath("~/" + uploadTo)))
                    Directory.CreateDirectory(Server.MapPath("~/" + uploadTo));

                string fname = filename + Path.GetExtension(Path.GetFileName(theFile.FileName));
                theFile.SaveAs(Server.MapPath(Path.Combine("~/" + uploadTo + "/", fname)));
            }
        }        

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            conndb db = new conndb();
            SqlConnection con = db.GetOpenConnection();
            SqlCommand cmd = con.CreateCommand();
            MessageBox.Show(ddlAdvPeriod.SelectedValue);
            int AdvPeriod = Convert.ToInt32(ddlAdvPeriod.SelectedValue);
            int AdvPictureSize = Convert.ToInt32(ddlAdvPictureSize.SelectedValue);
            string advName = tbAdvName.Text;
            string username = Session["username"].ToString();
            string img = AdvImage.ImageUrl.ToString();
            decimal totalprice = AdvPeriod * AdvPictureSize;

            if (advName != "" && img != "")
            {
                System.IO.File.Delete(Server.MapPath(img));
                HttpPostedFile theFile = Session["PostedFile"] as HttpPostedFile;
                uploadFile(theFile, "Advertisement", advName + "("+username+")");
                AdvImage.ImageUrl = "~/Advertisment/" + advName + ".jpg";
            }

            AdvertisementManagement.insertAdvertisement(username, img, AdvPictureSize, AdvPeriod, totalprice);             
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string username = "TempAdvertisement";
            string uploadTo = "Advertisement";

            if (AdvUpload.HasFile)
            {
                if (Directory.Exists(Server.MapPath("~/" + uploadTo + "/" + username)))
                {
                    System.IO.File.Delete(Server.MapPath("~/" + uploadTo + "/" + username));
                }

                else
                {
                    HttpPostedFile theFile = AdvUpload.PostedFile;
                    Session["PostedFile"] = theFile;
                    uploadFile(theFile, uploadTo, username);
                    AdvImage.ImageUrl = "~/" + uploadTo + "/" + username + ".jpg";
                }
            } 
        }

    }
}